(function($){
    window.DPLib = {
        VISIABLE:0,
        INVISIABLE:4,
        GONE:8,
        KEY_DOWN:0,
        KEY_UP:1,
        prompt:function(msg,defaultText){
            $('{"action":"prompt"}');
        },
        isEmpty:function(str){
            return "" == str || undefined == str || null == str || NaN == str;
        },
        arrayToJson:function(arr){
            var a = [];
            for(var i=0;i<arr.length;i++){
                a.push('"' + arr[i] + '"');
            }
            return '[' + a.join() + ']';
        },
        back:function(){
            $('{"action":"back"}');
        },
        open:function(url, type, title, backButtonVisibility){
            if(this.isEmpty(type)){
                type = "";
            }
            if(this.isEmpty(title)){
                title = "";
            }
            if(backButtonVisibility != this.VISIABLE && backButtonVisibility != this.INVISIABLE && backButtonVisibility != this.GONE){
                backButtonVisibility = this.VISIABLE;
            }
            $('{"action":"open",url:"' + url + '",type:"' + type + '",title:"' + title + '",backButtonVisibility:"' + backButtonVisibility + '"}');
        },
        openForResult:function(callback, reqCode, url, type, title, backButtonVisibility){
            if(this.isEmpty(type)){
                type = "";
            }
            if(this.isEmpty(title)){
                title = "";
            }
            if(backButtonVisibility != this.VISIABLE && backButtonVisibility != this.INVISIABLE && backButtonVisibility != this.GONE){
                backButtonVisibility = this.VISIABLE;
            }
            $('{"action":"openForResult",reqCode:"' + reqCode + '",callback:"' + callback + '",url:"' + url + '",type:"' + type + '",title:"' + title + '",backButtonVisibility:"' + backButtonVisibility + '"}');
        },
        finish:function(data){
            $('{"action":"finish",data:"' + data +  '"}');
        },
        toast:function(msg){
            $('{"action":"toast",msg:"' + msg + '"}');
        },
        setTitle:function(title){
            $('{"action":"setTitle",title:"' + title + '"}');
        },
        setBackButtonVisibility:function(visibility){
            $('{"action":"setBackButtonVisibility",visibility:"' + visibility + '"}');
        },
        setFunctionButtonText:function(text){
            this.setFunctionButtonVisibility(this.VISIABLE);
            $('{"action":"setFunctionButtonText",text:"' + text + '"}');
        },
        setFunctionButtonTextVisibility:function(visibility){
            $('{"action":"setFunctionButtonTextVisibility",visibility:' + visibility + '}');
        },
        setFunctionButtonImage:function(img){
            this.setFunctionButtonVisibility(this.VISIABLE);
            $('{"action":"setFunctionButtonImage",img:"' + img + '"}');
        },
        setFunctionButtonImageVisibility:function(visibility){
            $('{"action":"setFunctionButtonImageVisibility",visibility:' + visibility + '}');
        },
        setFunctionButtonCallback:function(callback, args){
            if(this.isEmpty(args)){
                args = '[]';
            }
            $('{"action":"setFunctionButtonCallback",callback:"' + callback + '",args:"' + args + '"}');
        },
        setFunctionButtonVisibility:function(visibility){
            $('{"action":"setFunctionButtonVisibility",visibility:' + visibility + '}');
        },
        setFunctionButtonClickable:function(clickable){
            $('{"action":"setFunctionButtonClickable",clickable:' + clickable + '}');
        },
        showDropDownMenu:function(items, callback){
            $('{"action":"showDropDownMenu",data:' + items + ',callback:"' + callback + '"}');
        },
        showShareDialog:function(title, content, url, img,surl){
            $('{"action":"showShareDialog",title:"' + title + '",content:"' + content + '",url:"' + url + '",img:"' + img + '",surl:"'+surl+'"}');
        },
        registerBackCallback:function(callback, preventDefault){
            $('{"action":"registerBackCallback",callback:"' + callback + '",preventDefault:' + preventDefault + '}');
        },
        registerResumeCallback:function(){
            $('{"action":"registerResumeCallback"}');
        },
        registerStopCallback:function(){
            $('{"action":"registerStopCallback"}');
        },
        saveData:function(key, data){
            $('{"action":"saveData",key:"' + key + '",data:"' + data + '"}');
        },
        getData:function(key, callback){
            $('{"action":"getData",key:"' + key+ '",callback:"' + callback + '"}');
        },
        cancelToast:function(){
            $('{"action":"cancelToast"}');
        }
    }
})(window.prompt);
//DPLib.custom("msg");